package com.cupk.rotaryguard.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cupk.common.base.utils.result.Result;
import com.cupk.common.base.utils.result.ResultGenerator;
import com.cupk.rotaryguard.monitor.entity.DeviceDiagnosis;
import com.cupk.rotaryguard.monitor.entity.DeviceInfo;
import com.cupk.rotaryguard.monitor.entity.WarningInfo;
import com.cupk.rotaryguard.monitor.mapper.DeviceMapper;
import com.cupk.rotaryguard.monitor.mapper.DiagnosisMapper;
import com.cupk.rotaryguard.monitor.mapper.WarningMapper;
import com.cupk.rotaryguard.monitor.service.WarningService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author tjy
 * @date 2023/4/17
 * @apiNote
 */
@Service
public class WarningServiceImpl extends ServiceImpl<WarningMapper, WarningInfo>
        implements WarningService {

    @Resource
    private WarningMapper warningMapper;
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private DiagnosisMapper diagnosisMapper;

    /**
     * 获取未处理预警列表
     *
     * @return
     */
    @Override
    public Result getTodoWarningList(Integer pageNum,Integer pageSize) {
        QueryWrapper<WarningInfo> warningInfoQueryWrapper = new QueryWrapper<>();
        warningInfoQueryWrapper.like("handling_status", 0);
        warningInfoQueryWrapper.orderByDesc("alert_time");
        IPage<WarningInfo> pageData = new Page<>(pageNum, pageSize);
        IPage<WarningInfo> warningInfoIPage = warningMapper.selectPage(pageData, warningInfoQueryWrapper);
        return ResultGenerator.genSuccessResult(warningInfoIPage, "查询成功");
    }

    /**
     * 获取所有预警列表
     *
     * @return
     */
    @Override
    public Result getWarningList(Integer pageNum, Integer pageSize,
                                 Long deviceId, String deviceModel,
                                 String deviceFault, String handlingPerson, Integer handlingStatus) {
        QueryWrapper<WarningInfo> warningInfoQueryWrapper = new QueryWrapper<>();
        if (null != deviceId) {
            warningInfoQueryWrapper.like("device_id", deviceId);
        }
        warningInfoQueryWrapper.like("device_model", deviceModel);
        warningInfoQueryWrapper.like("device_fault", deviceFault);
        warningInfoQueryWrapper.like("handling_person", handlingPerson);
        if (null != handlingStatus) {
            warningInfoQueryWrapper.like("handling_status", handlingStatus);
        }
        warningInfoQueryWrapper.orderByDesc("alert_time");
        IPage<WarningInfo> pageData = new Page<>(pageNum, pageSize);
        IPage<WarningInfo> warningInfoIPage = warningMapper.selectPage(pageData, warningInfoQueryWrapper);
        return ResultGenerator.genSuccessResult(warningInfoIPage, "查询成功");
    }

    /**
     * 处理预警
     *
     * @param warningId
     * @param username
     * @return
     */
    @Override
    public Result doWarningRead(Integer warningId, String username) {
        WarningInfo curWarning = warningMapper.selectById(warningId);
        if (ObjectUtils.isEmpty(curWarning)) {
            return ResultGenerator.genFailResult(null, "id为" + warningId + "的预警信息不存在");
        }
        if (curWarning.getHandlingStatus() == 1) {
            return ResultGenerator.genFailResult(null, "id为" + warningId + "的预警信息已处理");
        }
        curWarning.setHandlingStatus(1);
        curWarning.setHandlingPerson(username);
        curWarning.setHandlingTime(LocalDateTime.now());
        warningMapper.updateById(curWarning);
        return ResultGenerator.genSuccessResult(curWarning, "处理成功");
    }

    /**
     * 生成预警
     *
     * @return
     */
    @Override
    public Result generateWarning() {
        //获取设备号
        List<DeviceInfo> deviceInfos = deviceMapper.selectList(null);
        for (DeviceInfo device : deviceInfos) {
            //根据设备号获取最新状态诊断预测结果
            QueryWrapper<DeviceDiagnosis> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("device_id", device.getId())
                    .orderByDesc("diagnosis_time").last("limit 1");
            DeviceDiagnosis diagnosis = diagnosisMapper.selectOne(queryWrapper);
            String diagnosisRes = diagnosis.getDiagnosisRes();
            if (!diagnosisRes.equals("正常")) {
                //生成预警信息
                WarningInfo warningInfo = new WarningInfo();
                warningInfo.setDeviceId(device.getId());
                warningInfo.setDeviceModel(device.getDeviceModel());
                warningInfo.setDeviceFault(diagnosisRes);
                warningMapper.insert(warningInfo);
            }
        }
        return ResultGenerator.genSuccessResult(null, "生成预警信息成功");
    }

    /**
     * 根据id 单个删除
     *
     * @param id
     * @return
     */
    @Override
    public Result delWarning(Integer id) {
        warningMapper.deleteById(id);
        return ResultGenerator.genSuccessResult(null, "删除成功,id:" + id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public Result delWarnings(List<Integer> ids) {
        warningMapper.deleteBatchIds(ids);
        return ResultGenerator.genSuccessResult(null, "删除成功");
    }

    /**
     * 获取预警总数
     *
     * @return
     */
    @Override
    public Result warningCount() {
        QueryWrapper<WarningInfo> queryWrapper = new QueryWrapper<>();
        long count = count(queryWrapper);
        return ResultGenerator.genSuccessResult(count, "查询成功");

    }

    /**
     * 获取待处理预警总数
     *
     * @return
     */
    @Override
    public Result warningTodoCount() {
        QueryWrapper<WarningInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("handling_status", 0);
        long count = count(queryWrapper);
        return ResultGenerator.genSuccessResult(count, "查询成功");
    }
}
